<?php
$objCustomerForm = new BM_PHPForm();
$objCustomerForm->UseLocator();
$objCustomerForm->SetIndent("          ");
if ($objCustomerForm->IsPost()){
	$objCustomerForm->ChEmptiness("username", "Username is empty.");
	$objCustomerForm->ChWithRegex("username", "Username Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-])*$/");
	$objCustomerForm->ChMinLength("username", "Username must not be less than 4 chars", 4);
	$objCustomerForm->ChMaxLength("username", "Username must not be more than 40 chars.", 40);
	if($objCustomerForm->ChWithRegex("username", "Username Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-])*$/")){
		$entUser0 = UserController::find($objCustomerForm->arrPostData['username']);
		$objCustomerForm->ChCustomVal("username", "This Username Isn't Available ! Please Choose Another One." , $entUser0->username == "guest");
	}
	$objCustomerForm->ChEmptiness("password", "Password is empty.");
	$objCustomerForm->ChMinLength("password", "Password must be more than 4 chars", 4);
	$objCustomerForm->ChMaxLength("password", "Password must not be more than 12 chars.", 12);
	$objCustomerForm->ChCustomVal("password", "Passwords must be same.",$objCustomerForm->arrPostData['password']==$objCustomerForm->arrPostData['password2'] );
	$objCustomerForm->ChEmptiness("firstname","First Name is empty.");
	$objCustomerForm->ChWithRegex("firstname","First Name Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/");
	$objCustomerForm->ChEmptiness("lastname", "Last Name is empty.");
	$objCustomerForm->ChWithRegex("lastname", "Last Name Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/");
	$objCustomerForm->ChWithRegex("address"	, "Address have ['] Please Remove ' of your text.","/^([^'])*$/");
	$objCustomerForm->ChEmptiness("email"	, "Email is empty.");
	$objCustomerForm->ChWithRegex("email"	, "Email is Invalid.", "/^[a-z0-9_\\.\\-]+@+[a-z0-9_\\.\\-]+(\\.[a-z]{2,4})$/");
	$objCustomerForm->ChWithRegex("phone"	, "Phone have ['] Please Remove ' of your text.","/^([^'])*$/");
	$objCustomerForm->ChWithRegex("cell"	, "Cell have ['] Please Remove ' of your text.","/^([^'])*$/");
//	$objCustomerForm->ChEmptiness("birthday", "Birthday is empty.");
//	$objCustomerForm->ChWithRegex("birthday", "Date Format Must Be Like Day/Month/Year  (e.g. 20/05/1980)","/^[12]{1}[0-9]{3}+\-+[01]{1}[0-9]{1}+\-+[0123]{1}[0-9]{1}$/");
//	$objCustomerForm->ChEmptiness("type", "Please Select Category .");

	if ($objCustomerForm->IsValid()){
		$name = $objCustomerForm->arrPostData['firstname'] . " " . $objCustomerForm->arrPostData['lastname'];
		$entUser = new User($objCustomerForm->arrPostData['username'],md5($objCustomerForm->arrPostData['password']),$name);
		UserController::save($entUser);
		$uroles = array();
		$uroles[] = "ROLE_CUSTOMER";
		UserController::saveRoles($entUser,$uroles);
		$entCustomer = new Customer($objCustomerForm->arrPostData['username'],$objCustomerForm->arrPostData['address'],$objCustomerForm->arrPostData['email'],$objCustomerForm->arrPostData['phone'],$objCustomerForm->arrPostData['cell'],null,null);
		CustomerController::save($entCustomer);
		$entUser->addToRoles("ROLE_CUSTOMER");
		$_SESSION['X_USER'] = $entUser;		
		header("Location: ../$www/index_customer.php?action=profile&msg=add");
	}	
}

print $objCustomerForm->ErrMsgs();
print $objCustomerForm->InfMsgs();

print $objCustomerForm->Open("customerform"	,"Customer Registration Form");
print $objCustomerForm->HiddenField("action","$action");
print $objCustomerForm->TextBox("username" 	,"Username<span style=\"color:#cc0000;\">*</span>:","");
print $objCustomerForm->TextBox("password" 	,"Password<span style=\"color:#cc0000;\">*</span>:","",array(),"password");
print $objCustomerForm->TextBox("password2" ,"Re-Password<span style=\"color:#cc0000;\">*</span>:","",array(),"password");
print $objCustomerForm->TextBox("firstname" ,"First Name<span style=\"color:#cc0000;\">*</span>:","");
print $objCustomerForm->TextBox("lastname" 	,"Last Name<span style=\"color:#cc0000;\">*</span>:","");
print $objCustomerForm->TextBox("address" 	,"Address :","");
print $objCustomerForm->TextBox("email" 	,"E-mail<span style=\"color:#cc0000;\">*</span>:","");
print $objCustomerForm->TextBox("phone" 	,"Phone No :","");
print $objCustomerForm->TextBox("cell" 		,"Mobile No :","");
//print $objCustomerForm->TextBox("birthday" 	,"Date Of Birth:",""); 
//print $objCustomerForm->Select ("type" 		,"Category<span style=\"color:#cc0000;\">*</span>:",$arrCtype);

print $objCustomerForm->SubmitButton("submit", "Register", array(), "submit");
//print $objCustomerForm->Reset("reset", "Reset", array(),"reset");

print $objCustomerForm->Close();
?>